package com.itwmk.service.impl.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itwmk.entity.Category;
import com.itwmk.entity.Dish;
import com.itwmk.entity.Setmeal;
import com.itwmk.mapper.CategoryMapper;
import com.itwmk.service.impl.CategoryService;
import com.itwmk.service.impl.DishService;
import com.itwmk.service.impl.SetmealServic;
import com.itwmk.utils.CustomException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealServic setmealServic;
    @Override
    public void myRemoveById(Long id) {
        LambdaQueryWrapper<Dish> dishWrapper = new LambdaQueryWrapper<>();
        dishWrapper.eq(Dish::getCategoryId,id);
        int dishCount = dishService.count(dishWrapper);
        if (dishCount>0){
            throw new CustomException("无法删除，当前分类项已有关联");
        }

        LambdaQueryWrapper<Setmeal> setmealWrapper = new LambdaQueryWrapper<>();
        setmealWrapper.eq(Setmeal::getCategoryId,id);
        int setmealCount = setmealServic.count(setmealWrapper);
        if (setmealCount>0){
            throw new CustomException("无法删除，当前分类项已有关联");
        }

        removeById(id);

    }
}
